package com.yanqu.road.server.manger.activity.summerquiz;

import com.yanqu.road.dao.impl.activity.summerquiz.SummerQuizConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizConfig;
import com.yanqu.road.entity.activity.summerquiz.config.SummerQuizInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.pb.activity.SummerQuizProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summerquiz.SummerQuizModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SummerQuizActivityMgr extends TempMgr {

    private static SummerQuizConfig config;

    private static Map<Integer, Set<Long>> settleMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        settleMap = new ConcurrentHashMap<>();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload SummerQuiz Activity start");
        ActivityInfo tempActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SummerQuiz.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no SummerQuiz Activity in show time");
            config = null;
        } else {

            tempActivityInfo = openActivityInfoList.get(0);
            int tmpActivityId = tempActivityInfo.getActivityId();

            SummerQuizConfig configTemp = new SummerQuizConfig(tempActivityInfo);

            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

            SummerQuizConfigDaoImpl dao = new SummerQuizConfigDaoImpl();

            configTemp.setOddsRangeList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_ODDS_RANGE.getName()).getValue(), "\\|"));
            configTemp.setSUMMERQUIZ_ODDS_CHANGE_TIME(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_ODDS_CHANGE_TIME.getName()).getIntValue());
            configTemp.setOddsParamList(StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_ODDS_PARAM.getName()).getValue(), "\\|"));

            configTemp.setSUMMERQUIZ_BULLIONS_ANTE(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_BULLIONS_ANTE.getName()).getIntValue());
            configTemp.setSUMMERQUIZ_LOSER_GET_BULLIONS(activityConfigMap.get(eActivityConfigType.SUMMERQUIZ_LOSER_GET_BULLIONS.getName()).getIntValue());

            configTemp.setQuizInfoList(dao.getSummerQuizInfoList(tmpActivityId));

            config = configTemp;
            if(!settleMap.containsKey(tmpActivityId)){
                settleMap.put(tmpActivityId, new HashSet<>());
            }

            try{
                //通知跨服加载配置
                Long sid = tempActivityInfo.getServerIdList().get(0);
                if(GameServer.getInstance().getServerId() == sid){
                    GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_SUMMER_QUIZ_RELOAD_CONFIG,
                            SummerQuizProto.CrossSummerQuizReloadConfigReqMsg.newBuilder().setActivityId(tmpActivityId)));
                }
            }catch (Exception e){
                getLogger().error("", e);
            }
        }

        getLogger().info("reload SummerQuiz Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            SummerQuizModule module = gamePlayer.getModule(SummerQuizModule.class);
            module.activityReload();
        }
    }

    public static void beginSettleGuess(){
        if(config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())){
            return;
        }
        long yesterdayZeroTime = DateHelper.getTodayZeroTimeStamp() - DateHelper.DAY_MILLIONS;
        SummerQuizInfo settleGuess = config.findLastNeedSettleGuess(yesterdayZeroTime);
        if(settleGuess == null){
            return;
        }
        int activityId = config.getActivityInfo().getActivityId();
        if(!settleMap.containsKey(activityId)){
            settleMap.put(activityId, new HashSet<>());
        }
        if(settleMap.get(activityId).contains(yesterdayZeroTime)){
            return;
        }
        SummerGamesConfig gamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        if(gamesConfig == null || !ActivityMgr.activityInShowTime(gamesConfig.getActivityInfo())){
            return;
        }
        getLogger().info("开始结算竞猜，夏日竞猜活动 {}，夏日活动 {}, 日期 {}", activityId, gamesConfig.getActivityId(), yesterdayZeroTime);

        SummerQuizProto.CrossSummerQuizGuessSettleReqMsg.Builder crossMsg = SummerQuizProto.CrossSummerQuizGuessSettleReqMsg.newBuilder();
        crossMsg.setActivityId(activityId).setQuizId(settleGuess.getId()).setGameActivityId(gamesConfig.getActivityId());
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_SUMMER_QUIZ_SETTLE, crossMsg));
    }

    public static SummerQuizConfig getConfig() {
        return config;
    }
}
